Skip to content
This repository was archived by the owner on Dec 1, 2021. It is now read-only.

Implemented easydict. #1094

Merged
merged 9 commits into from
Jun 25, 2020
Merged

Conversation

odoku
Copy link
Contributor

@odoku odoku commented Jun 16, 2020

What this patch does to fix the issue.

#1092

@blueoil-butler blueoil-butler bot added the CI: auto-run Run CI automatically label Jun 16, 2020
@CLAassistant
Copy link

CLAassistant commented Jun 16, 2020

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ odoku
❌ bo-mergebot[bot]
You have signed the CLA already but the status is still pending? Let us recheck it.

@bo-code-review-bot
Copy link

This PR needs Approvals as follows.

  • Ownership Approval for / from iizukak, tkng, ruimashita
  • Readability Approval for Python from tkng, tsawada, tfujiwar

Please choose reviewers and requet reviews!

Click to see how to approve each reviews

You can approve this PR by triggered comments as follows.

  • Approve all reviews requested to you (readability and ownership) and LGTM review
    Approval, LGTM

  • Approve all ownership reviews
    Ownership Approval or OA

  • Approve all readability reviews
    Readability Approval or RA

  • Approve specified review targets

    • Example of Ownership Reviewer of /: Ownership Approval for / or OA for /
    • Example of Readability Reviewer of Python: Readability Approval for Python or RA for Python
  • Approve LGTM review
    LGTM

See all trigger comments

Please replace [Target] to review target

  • Ownership Approval
    • Ownership Approval for [Target]
    • OA for [Target]
    • Ownership Approval
    • OA
    • Approval
  • Readability Approval
    • Readability Approval for [Target]
    • RA for [Target]
    • [Target] Readability Approval
    • [Target] RA
    • Readability Approval
    • RA
    • Approval
  • LGTM
    • LGTM
    • lgtm

@odoku odoku self-assigned this Jun 16, 2020
Copy link
Member

@iizukak iizukak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good except license header.

@@ -0,0 +1,30 @@
class SmartDict(dict):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add license header.

@@ -0,0 +1,63 @@
import pytest
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

@iizukak iizukak requested a review from tfujiwar June 17, 2020 01:34
@iizukak
Copy link
Member

iizukak commented Jun 17, 2020

@tfujiwar Please review 🙇

@tfujiwar tfujiwar requested review from a-hanamoto and removed request for tfujiwar June 17, 2020 02:24
@a-hanamoto
Copy link

I'll check tomorrow so please wait for a while. Thanks!

super(SmartDict, self).__init__()
self.update(d or {}, **kwargs)

def update(self, d=None, **kwargs):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this method non-public? _update

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@a-hanamoto
Since the update function is in dict, I think it is necessary for SmartDict which inherits dict.
https://docs.python.org/ja/3/library/stdtypes.html?highlight=dict#dict.update

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then could you please write test cases for update?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added test case for update function. Please review it.

class SmartDict(dict):
def __init__(self, d=None, **kwargs):
super(SmartDict, self).__init__()
self.update(d or {}, **kwargs)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

d will be replaced d or {} soon after calling update, so d is OK, I think.

def __setitem__(self, name, value):
if isinstance(value, (list, tuple)):
value = [
self.__class__(x) if isinstance(x, dict) else x

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SmartDict? I wonder why there are both of self.__class__ and SmartDict

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@a-hanamoto
Your question is, why do you write "self.class(x)" instead of "SmartDict(x)"?
This is because if you inherit "SmartDict", "self.class" will be a subclass.

class HyperSmartDict(SmartDict):
    pass

d = HyperSmartDict()
d.__class__ == HyperSmartDict

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got your point. Thanks!

@odoku odoku requested a review from a-hanamoto June 18, 2020 10:25
super(SmartDict, self).__init__()
self.update(d or {}, **kwargs)

def update(self, d=None, **kwargs):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then could you please write test cases for update?

def __setitem__(self, name, value):
if isinstance(value, (list, tuple)):
value = [
self.__class__(x) if isinstance(x, dict) else x

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got your point. Thanks!

@odoku odoku requested a review from a-hanamoto June 19, 2020 07:41
Copy link

@a-hanamoto a-hanamoto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RA (reverse-shadowing)
Thank you!

@a-hanamoto a-hanamoto requested a review from tfujiwar June 19, 2020 13:10
def __getattr__(self, name):
if name in self:
return self[name]
return super(SmartDict, self).__getattr__(name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think __getattr__ should raise an AttributeError instead of delegating to the super class. Because Dict doesn't have a method __getattr__, the current error message is like:

AttributeError: 'super' object has no attribute '__getattr__'

It should be like:

AttributeError: 'SmartDict' object has no attribute 'x'

Comment on lines 45 to 47
if hasattr(self, name):
return super(SmartDict, self).__setattr__(name, value)
self[name] = value
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think self[name] = value is enough. Currently, the results are different for an existing and non-existing key.

d = SmartDict(a=None)
d.a = [{"k":"v"}]
d.b = [{"k":"v"}]

d.a[0].__class__  # dict
d.b[0].__class__  # SmartDict

@odoku odoku requested a review from tfujiwar June 24, 2020 11:39
Copy link
Contributor

@tfujiwar tfujiwar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RA
Thanks for revising.

@iizukak
Copy link
Member

iizukak commented Jun 25, 2020

/ready

@bo-mergebot
Copy link
Contributor

bo-mergebot bot commented Jun 25, 2020

⏳Merge job is queued...

@bo-mergebot
Copy link
Contributor

bo-mergebot bot commented Jun 25, 2020

😥This PR is not approved yet.

@iizukak
Copy link
Member

iizukak commented Jun 25, 2020

OA

@iizukak
Copy link
Member

iizukak commented Jun 25, 2020

/ready

@bo-mergebot
Copy link
Contributor

bo-mergebot bot commented Jun 25, 2020

⏳Merge job is queued...

@bo-mergebot bo-mergebot bot merged commit aee0301 into blue-oil:master Jun 25, 2020
@odoku odoku deleted the reimplement-easydict branch June 25, 2020 06:16
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
CI: auto-run Run CI automatically
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants